/*
* Sys.java
* Copyright (C) 2003
* 
* $Id: Sys.java,v 1.11 2005/07/01 14:20:54 hzi Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
using System;
using Defines = Suake2.UI.Defines;
using Globals = Suake2.UI.Globals;
using CL = Suake2.UI.client.CL;
using Com = Suake2.UI.qcommon.Com;
//UPGRADE_TODO: The type 'java.util.regex.Pattern' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using Pattern = java.util.regex.Pattern;
//UPGRADE_TODO: The type 'java.util.regex.PatternSyntaxException' could not be found. If it was not included in the conversion, there may be compiler issues. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1262'"
using PatternSyntaxException = java.util.regex.PatternSyntaxException;
namespace Suake2.UI.sys
{
	
	/// <summary> Sys</summary>
	public sealed class Sys:Defines
	{
		
		public static void  Error(System.String error)
		{
			
			CL.Shutdown();
			//StackTrace();
			SupportClass.WriteStackTrace(new System.Exception(error), Console.Error);
			System.Environment.Exit(1);
		}
		
		public static void  Quit()
		{
			CL.Shutdown();
			
			System.Environment.Exit(0);
		}
		
		//ok!
		public static System.IO.FileInfo[] FindAll(System.String path, int musthave, int canthave)
		{
			
			int index = path.LastIndexOf('/');
			
			if (index != - 1)
			{
				findbase = path.Substring(0, (index) - (0));
				findpattern = path.Substring(index + 1, (path.Length) - (index + 1));
			}
			else
			{
				findbase = path;
				findpattern = "*";
			}
			
			if (findpattern.Equals("*.*"))
			{
				findpattern = "*";
			}
			
			System.IO.FileInfo fdir = new System.IO.FileInfo(findbase);
			
			bool tmpBool;
			if (System.IO.File.Exists(fdir.FullName))
				tmpBool = true;
			else
				tmpBool = System.IO.Directory.Exists(fdir.FullName);
			if (!tmpBool)
				return null;
			
			//UPGRADE_ISSUE: Interface 'java.io.FilenameFilter' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioFilenameFilter'"
			FilenameFilter filter = new FileFilter(findpattern, musthave, canthave);
			
			//UPGRADE_ISSUE: Method 'java.io.File.listFiles' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioFilelistFiles_javaioFilenameFilter'"
			return fdir.listFiles(filter);
		}
		
		/// <summary> Match the pattern findpattern against the filename.
		/// 
		/// In the pattern string, `*' matches any sequence of characters, `?'
		/// matches any character, [SET] matches any character in the specified set,
		/// [!SET] matches any character not in the specified set. A set is composed
		/// of characters or ranges; a range looks like character hyphen character
		/// (as in 0-9 or A-Z). [0-9a-zA-Z_] is the set of characters allowed in C
		/// identifiers. Any other character in the pattern must be matched exactly.
		/// To suppress the special syntactic significance of any of `[]*?!-\', and
		/// match the character exactly, precede it with a `\'.
		/// </summary>
		//UPGRADE_ISSUE: Interface 'java.io.FilenameFilter' was not converted. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1000_javaioFilenameFilter'"
		internal class FileFilter : FilenameFilter
		{
			
			internal System.String regexpr;
			
			internal int musthave, canthave;
			
			internal FileFilter(System.String findpattern, int musthave, int canthave)
			{
				this.regexpr = convert2regexpr(findpattern);
				this.musthave = musthave;
				this.canthave = canthave;
			}
			
			/*
			* @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
			*/
			public bool accept(System.IO.FileInfo dir, System.String name)
			{
				if (name.matches(regexpr))
				{
					return CompareAttributes(dir, musthave, canthave);
				}
				return false;
			}
			
			internal System.String convert2regexpr(System.String pattern)
			{
				
				System.Text.StringBuilder sb = new System.Text.StringBuilder();
				
				char c;
				bool escape = false;
				
				System.String subst;
				
				// convert pattern
				for (int i = 0; i < pattern.Length; i++)
				{
					c = pattern[i];
					subst = null;
					switch (c)
					{
						
						case '*': 
							subst = (!escape)?".*":"*";
							break;
						
						case '.': 
							subst = (!escape)?"\\.":".";
							break;
						
						case '!': 
							subst = (!escape)?"^":"!";
							break;
						
						case '?': 
							subst = (!escape)?".":"?";
							break;
						
						case '\\': 
							escape = !escape;
							break;
						
						default: 
							escape = false;
							break;
						
					}
					if (subst != null)
					{
						sb.Append(subst);
						escape = false;
					}
					else
						sb.Append(c);
				}
				
				// the converted pattern
				System.String regexpr = sb.ToString();
				
				//Com.DPrintf("pattern: " + pattern + " regexpr: " + regexpr +
				// '\n');
				try
				{
					Pattern.compile(regexpr);
				}
				catch (PatternSyntaxException e)
				{
					Com.Printf("invalid file pattern ( *.* is used instead )\n");
					return ".*"; // the default
				}
				return regexpr;
			}
			
			internal bool CompareAttributes(System.IO.FileInfo dir, int musthave, int canthave)
			{
				// . and .. never match
				System.String name = dir.Name;
				
				if (name.Equals(".") || name.Equals(".."))
					return false;
				
				return true;
			}
		}
		
		
		//============================================
		
		internal static System.IO.FileInfo[] fdir;
		
		internal static int fileindex;
		
		internal static System.String findbase;
		
		internal static System.String findpattern;
		
		// ok.
		public static System.IO.FileInfo FindFirst(System.String path, int musthave, int canthave)
		{
			
			if (fdir != null)
				Sys.Error("Sys_BeginFind without close");
			
			//	COM_FilePath (path, findbase);
			
			fdir = FindAll(path, canthave, musthave);
			fileindex = 0;
			
			if (fdir == null)
				return null;
			
			return FindNext();
		}
		
		public static System.IO.FileInfo FindNext()
		{
			
			if (fileindex >= fdir.Length)
				return null;
			
			return fdir[fileindex++];
		}
		
		public static void  FindClose()
		{
			fdir = null;
		}
		
		public static void  SendKeyEvents()
		{
			Globals.re.KeyboardHandler.Update();
			
			// grab frame time
			Globals.sys_frame_time = Timer.Milliseconds();
		}
		
		public static System.String GetClipboardData()
		{
			// TODO: implement GetClipboardData
			return null;
		}
		
		public static void  ConsoleOutput(System.String msg)
		{
			if (Globals.nostdout != null && Globals.nostdout.value_Renamed != 0)
				return ;
			
			System.Console.Out.Write(msg);
		}
	}
}